package cn.rongdao.jrkabao.ui.deposit.manager;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.inputmethod.FRView;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.deposit.SelectCollectionCardBean;
import cn.rongdao.jrkabao.model.deposit.SelectCardFromKamaoModel;
import cn.rongdao.jrkabao.model.deposit.SelectCollectionCardModel;
import cn.rongdao.jrkabao.ui.cashadvance.addcard.AddDepositCardActivity;
import cn.rongdao.jrkabao.ui.deposit.DepositActivity;
import cn.rongdao.jrkabao.ui.deposit.adapter.DepositManagerAdapter;
import cn.rongdao.jrkabao.ui.deposit.adapter.DepositManagerViewHolder;
import cn.rongdao.jrkabao.ui.deposit.event.DepositAddCardEvent;
import cn.rongdao.jrkabao.ui.main.mainfragment.loanfragment.uicomponent.CheckAllProductUIComponent;

/**
 * Created by jack on 2017/7/29
 */

public class DepositManagerActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<DepositManagerViewHolder> {

    public static void start(Context context) {
        FRStartActivity.start(context, DepositManagerActivity.class);
    }

    @BindView(R.id.adm_titlebar)
    TitleBarUIComponent titleBarUIComponent;

    @BindView(R.id.adm_tv_tip)
    TextView tvTip;
    @BindView(R.id.adm_ptr_framelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;

    private DepositManagerAdapter adapter;
    private RecyclerAdapterWithHF mAdapter;
    private CheckAllProductUIComponent viewFooter;

    private String bankId;

    @Override
    protected void initView() {
        initTitleBar();
        initPtr();
        initData();
        initListener();
    }

    private void initPtr() {
        ptrRecyclerViewUIComponent.setCanRefresh(false);
        adapter = new DepositManagerAdapter(this, this);
        viewFooter = FRView.inflateLayout(thisActivity(), R.layout.layout_loan_footer_view);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        viewFooter.setAddCardIsEnable(false);
        mAdapter.addFooter(viewFooter);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);
    }

    private void initData() {
        SelectCollectionCardModel.getInstance().execute(new FRBaseObserver<List<SelectCollectionCardBean>>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(List<SelectCollectionCardBean> data) {
                if (null != data && data.size() > 0) {
                    setPtrVisible(true);
                    adapter.setDataList(data);
                    mAdapter.notifyDataSetChanged();
                } else {
                    setPtrVisible(false);
                }
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                setPtrVisible(false);
            }
        });
    }

    private void setPtrVisible(boolean isVisible) {
        ptrRecyclerViewUIComponent.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        viewFooter.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        viewFooter.setAddCardContent("已选好，去提现");
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void depositAddCardEventMessage(DepositAddCardEvent depositAddCardEvent) {
        finish();
    }

    private void initListener() {
        viewFooter.setOnAddCardClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectCardFromKamaoModel.getInstance().execute(bankId, new FRBaseObserver<List<Void>>(DepositManagerActivity.this, "正在绑定,请稍候...") {
                    @Override
                    protected void onBaseNext(List<Void> data) {
                        EventBus.getDefault().post(new DepositAddCardEvent());
                        DepositActivity.start(thisActivity());
                        finish();
                    }

                    @Override
                    protected void onBaseError(Throwable t) {
                        super.onBaseError(t);
                        viewFooter.setEnabled(true);
                    }
                });
            }
        });
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("选择收款卡");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
        titleBarUIComponent.initRightTextWithColor("添加", R.color.white, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddDepositCardActivity.start(thisActivity(), true, false);
            }
        });
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_deposit_manager;
    }

    @Override
    public void onItemClick(DepositManagerViewHolder depositManagerViewHolder, int position) {
        viewFooter.setAddCardIsEnable(true);
        bankId = adapter.getDataList().get(position).getId();
        adapter.setSelect(position);
    }
}
